<?php

// +----------------------------------------------------------------------
// | Admin Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2024 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-admin
// | github 代码仓库：https://github.com/zoujingli/think-plugs-admin
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\paths\controller;

use app\common\model\admin\AccountModel;
use app\common\model\index\CategoryModel;
use app\common\model\paths\TaskModel;
use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemUser;
use think\admin\service\AdminService;
use think\facade\Validate;

/**
 * 课程控制器
 * @class Base
 * @package app\admin\controller
 */
class Task extends Controller
{

    /**
     * 课程列表
     * @auth true
     * @menu true
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index ()
    {

        TaskModel::mQuery()->layTable(function () {

            $this->title = '课程管理';
            $this->teacher_arr = SystemUser::mk()->where('account_id', $this->account_id)->where('usertype', 'teacher')->where('is_deleted' , 0)->column('nickname', 'id');

        }, function (QueryHelper $query) {

            $query->order('id', 'desc');
            // 数据列表搜索过滤
            $query->like('title')->equal('status,account_id,user_id')->dateBetween('create_at');
        });
    }

    protected function _index_page_filter (&$data)
    {

        $teacher_ids = $account_ids =[];
        array_map(function ($item) use (&$account_ids, &$teacher_ids) {

            $teacher_ids[]   = $item['user_id'];
            $account_ids[]   = $item['account_id'];
        }, $data);

        $account_arr    = $this->super ? $this->account_arr : AccountModel::mk()->whereIn("id",  array_unique($account_ids))->column('title', 'id');
        $teacher_arr     = SystemUser::mk()->whereIn("id", array_unique($teacher_ids))->column('nickname', 'id');

        foreach ($data as &$item) {

            $item['account_name']   = $account_arr[$item['account_id']] ?? '';
            $item['user_name']       = $teacher_arr[$item['user_id']] ?? '';
        }
    }
    /**
     * 添加
     * @auth true
     * @return void
     */
    public function add ()
    {
        TaskModel::mForm();
    }

    /**
     * 编辑
     *
     * @return void
     */
    public function edit ()
    {
        TaskModel::mForm();
    }

    /**
     * 处理表单数据
     * @author wangqinghui
     * @DateTime 2024-09-20 09:15:05
     * @param array $vo
     * @return void
     */
    protected function _form_filter(array &$data)
    {
        if($this->request->isPost()) {

            $params = $this->request->post();
            $rule = [
                'title|标题'    =>  'require|length:1,255',
                'logo|封面图'   =>  'require|length:1,255',
                'details|简介'  =>  'length:1,255',
                'sub_category_id|所属分类' =>  'require|number'
            ];
            $validate = Validate::rule($rule);

            if (!$validate->check($params)) {
                $this->error($validate->getError());
            }

            // 获取分类数据
            $category_info = CategoryModel::mk()->where('id', $params['sub_category_id'])->find();
            $data['category_id'] = $category_info['pid'] ?? 0;
            $data['user_id']    = $data['user_id'] ?? $this->adminUser['id'];
            $data['account_id'] = $data['account_id'] ?? $this->account_id;
        } else {

            $category_list       = CategoryModel::mk()->field('id,pid,name')->where('status', 1)->select()->toArray();
            $this->category_data = DataExtend::arr2tree($category_list);

            $account = $data['account_id'] ?? $this->account_id;
            $this->teacher_arr   = SystemUser::mk()->where('account_id', $account)->where('usertype', 'teacher')->where('is_deleted' , 0)->column('nickname', 'id');
            //$this->checkAuth(empty($data) ? AdminService::getPlatformId() : $data['platform_id'] );
        }
    }

    /**
     * 修改用户状态
     * @auth true
     */
    public function state()
    {

        TaskModel::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除系统用户
     * @auth true
     */
    public function remove()
    {

        TaskModel::mDelete();
    }
}